package com.kitty.game.equip.handler;

import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.FieldValuePosConst;
import com.kitty.common.model.Reason;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**法宝洗练*/
@Component("EquipRefineArtifactHandler")
public class EquipRefineArtifactHandler extends UpgradeHandler {
    private final static String ERROR_ITEM = "放入的物品不是法宝，请重新选择。";
    private final static String NOT_MATCH = "请放入同种类法宝。";
    private final static String GOLD_NOT_ENOUGH = "你的元宝不足。";
    private final static String LEVEL_NOT_ENOUGH = "主法宝等级达到#R7级#n后才可进行洗炼。";
    private final static String SUCCESS = "法宝洗练成功。";
    private final static int NEED_GOLD = 466;

    private final static List<String> SKILLS = Arrays.asList("diandao_qiankun", "jingangquan", "wuji_bifan", "tianyan", "chaofeng", "qinmi_wujian");

    /**
     * 开始打造 返回打造成功或者失败
     *
     * @param role
     * @param params
     */
    @Override
    public String doUpgrade(Role role, Object... params) {
        RoleEquip mainArtifact = (RoleEquip) params[0];
        RoleEquip firstEquip = (RoleEquip) params[1];
        RoleEquip secondEquip = (RoleEquip) params[2];
        /**删除副法宝*/
        equipService.delRoleEquip(firstEquip,role);
        equipService.delRoleEquip(secondEquip,role);
        /**扣除元宝 todo 好像是固定扣除466元宝*/
        roleService.subtractGold(role,NEED_GOLD, Reason.REFINE_ARTIFACT);

        mainArtifact.setEquipValue(FieldValuePosConst.ARTIFACT_SKILL, SKILLS.get(new Random().nextInt(SKILLS.size())));
        mainArtifact.setEquipValue(FieldValuePosConst.ARTIFACT_SKILL_LEVEL, 1);
        refreshRoleEquip(role, mainArtifact);
        pushOperateResult(role,UPGRADE_SUCCESS);
        return SUCCESS;
    }

    /**
     * 入口
     *
     * @param role
     * @param upgradePos
     * @param param
     */
    @Override
    public String acceptUpgrade(Role role, int upgradePos, String param) {
        String[] params = param.split("\\|");
        RoleEquip mainArtifact = equipService.getRoleEquipByPos(role, upgradePos);
        RoleEquip firstEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[0]));
        RoleEquip secondEquip = equipService.getRoleEquipByPos(role, Integer.parseInt(params[1]));

        /**判断是不是法宝*/
        if (!isArtifact(mainArtifact, firstEquip, secondEquip)) {
            return ERROR_ITEM;
        }

        /**判断副法宝跟主法宝是不是同种类*/
        if(!mainArtifact.getName().equals(firstEquip.getName()) || !mainArtifact.getName().equals(secondEquip.getName())){
            return NOT_MATCH;
        }


        /**判断主法宝登记是否足够*/
        if(mainArtifact.queryLevel()<7){
           return LEVEL_NOT_ENOUGH;
        }

        /**判断元宝是否足够*/
        if(role.getGold()<NEED_GOLD){
            return GOLD_NOT_ENOUGH;
        }


        return doUpgrade(role,mainArtifact,firstEquip,secondEquip);
    }

    /**
     * 判断传入的是不是法宝
     */
    private boolean isArtifact(RoleEquip... roleEquips) {
        for (RoleEquip roleEquip : roleEquips) {
            if (roleEquip == null || !roleEquip.isArtifact()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 检查需要的资源是否足够
     *
     * @param objects
     */
    @Override
    public String checkResource(Object... objects) {
        return null;
    }

    /**
     * 打造需要的金钱
     * pos 只在鉴定的时候用
     *
     * @param level
     * @param pos
     */
    @Override
    public int needCash(short level, int pos) {
        return 0;
    }

    /**
     * 取操作类型的字符串
     */
    @Override
    public String getUpdateTypeStr() {
        return "refine_artifact";
    }
}
